package com.runa.rccprotocol.analysis;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorResponseData;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RunaRepeaterAnalysis extends MetaBufferAnalysis<byte[]> {

    private final MetaBufferAnalysis<byte[]> analysis = new RunaDeviceAnalysis();

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        if ((data[0]&0xff) == 0x2a && (data[1]&0xff) == 0x2a) {
            int len = data[2] & 0xff;
            if (len == length) {
                int ctrl = data[3] & 0xff;
                if (ctrl == 0x80) {
                    int seq = data[4];
                    int series = data[5] & 0x0f;
                    String fmu = ByteUtil.byteToHexStringFlip(data, 6, 4);
                    int index = 0;
                    for (int i=0; i<series; i++) {
                        index += 4;
                    }
                    String target = ByteUtil.byteToHexStringFlip(data, 10+index, 4);
                    byte bl = data[14+index];
                    int dataLen = data[15+index] & 0xff;
                    byte[] bytes = new byte[dataLen];
                    System.arraycopy(data, 16+index, bytes, 0, dataLen);
                    analysis.fill(bytes, dataLen, netEquNo);
                    responseData = (IResponseData) analysis.gain();
                    index += 16 + dataLen;
                    Map<String, Object> signPacket = new HashMap<>(series+1);
                    Map<String, Integer> targetSign = new HashMap<>(2);
                    targetSign.put("rssi", - (data[index++] & 0xff));
                    targetSign.put("snr", (int) data[index++]);
                    signPacket.put("target", targetSign);
                    while (series > 0) {
                        Map<String, Integer> repeatSign = new HashMap<>(2);
                        repeatSign.put("rssi", - (data[index++] & 0xff));
                        repeatSign.put("snr", (int) data[index++]);
                        signPacket.put("repeat-"+series, repeatSign);
                        series--;
                    }
                    ConcentratorResponseData resultResponseData = (ConcentratorResponseData) responseData;
                    resultResponseData.setAddress(fmu);
                    if (responseData instanceof RunaConcenResponseData) {
//                        List<ValveVO> equData = (List<ValveVO>) resultResponseData.getData();
//                        for (ValveVO pojo : equData) {
//                            pojo.setSignal(signPacket.toString());
//                        }
                        List<Object> equData = (List<Object>) resultResponseData.getData();
                        for (Object vo : equData) {
                            if (vo instanceof ValveVO){
                                ValveVO valveVO = (ValveVO) vo;
                                valveVO.setSignal(signPacket.toString());
                            }else if (vo instanceof HotMeterVO){
                                HotMeterVO hotMeterVO = (HotMeterVO) vo;
                                hotMeterVO.setSignInfo(signPacket.toString());
                            }

                        }
                    }
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if ((data[i] & 0xff) == 0xFE) {
                    continue;
                }
                if ((data[i] & 0xff) == 0x68 && (data[i+1] & 0xff) == 0x81) {
                    if (data[length - 2] == ByteUtil.sum(data, i, length - i - 2)) {
                        int c = data[i + 9] & 0xff;
                        if (c == 0x84 && (data[i + 10] & 0xff) == 0x03
                                && (data[i + 11] & 0xff) == 0x1F && (data[i + 12] & 0xff) == 0x90) {
                            responseData = new RunaConcenAffirmDeny(true);
                        }
                    }
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        RunaRepeaterAnalysis runaRepeaterAnalysis = new RunaRepeaterAnalysis();
        DtuHDBufferAnalysis hdBufferAnalysis = new DtuHDBufferAnalysis(runaRepeaterAnalysis);
//        byte[] bytes = ByteUtil.hexStringToByte("7B09006F31333938303830320000002A2A5F8000000208981376300321004C687176300321001111813F1F9000FF9F000D005703E80000001403E7017F00000000081000010000423014020121000000000E2F0001276000020000000007C00000000000010001011104167BFC937B");
//        byte[] bytes = ByteUtil.hexStringToByte("7B09006F31333938303830320000002A2A5F8000000208981376300321004C687176300321001111813F1F9000FF9F000D005703E80000001403E7017F00000000081000010000423014020121000000000E2F0001276000020000000007C00000000000010001011104167BFC937B");

//        byte[] bytes = ByteUtil.hexStringToByte("7B09006F31343138303331300000002A2A5F8000001003181401000080004C687101000080004D22813F1F9000FFF7000F001403E80000003E03E700FD00000000980000010000034212200421101504010E3D0009276000020000000265C9000200000005000001DBB0161506D07B    ".replace(" ","").replace("\\n",""));
        byte[] bytes = ByteUtil.hexStringToByte("7B09006F31333938303831300000002A2A5F8000001008981310402121004C687110402121001111813F1F9000FFAD000E0005002A0000001B03E7015600000000080000010000214817231022000000000E3D004F276000020000000E971A0000000000010000018E91165C036B7B    ".replace(" ","").replace("\\n",""));
//        byte[] bytes = ByteUtil.hexStringToByte("7B09006F31333938303437340000002A2A5F8000007404981356610221004C687156610221001111813F1F9000FFDC000E005F03B50000001B03E700B000000000080000010000514210180921000000000E2F0016276000020000000657C20000000000010000018A6E16330AAB7B");
        hdBufferAnalysis.fill(bytes, bytes.length,"");
        hdBufferAnalysis.gain();
    }
}
